#include<iostream>
using namespace std;
class Base
{
public:
	Base(int b = 0)
		: _b(b)
	{}

	
	Base(const Base& b)
		: _b(b._b)
	{}

	Base& operator=(const Base& b)
	{
		if (this != &b)
		{
			_b = b._b;
		}

		return *this;
	}
protected:
	int _b;
};

class Derived : public Base
{
public:
	Derived(int b, int d)
		:Base(b)
		,_d(d)
	{
		_p = new int[10];
	}

	~Derived()
	{
		delete[] _p;
	}

	Derived(const Derived& d)
		: Base(d)
		, _d(d._d)
	{
		_p = new int[10];
	}

	Derived& operator=(const Derived& d)
	{
		if (this != &d)
		{
			// 不能用d中Base的部分给 *this 中 Base部分 赋值
			// *this = d;  // 无限递归
			Base::operator=(d);

			// 子类自己新增加的成员赋值
			_d = d._d;
			delete[] _p;
			_p = new int[10];
			// 下一步是 d._p空间中的内容拷贝到_p
		}

		return *this;
	}
protected:
	int _d;
	int* _p;
};


int main()
{
	Derived d1(1, 2);
	Derived d2(d1);

	Derived d3(4, 5);
	d1 = d3;
	return 0;
}
class Base
{
public:
	Base(int b = 0)
		: _b(b)
	{}

	~Base()
	{
		cout << "Base::~Base()" << endl;
	}
protected:
	int _b;
};

class Derived : public Base
{
public:
	Derived(int b, int d)
		: Base(b)
		, _d(d)
	{}

	~Derived()
	{
		cout << "Derived::~Derived()" << endl;

		// 编译器在子类析构函数最后一个语句之后
		// call Base::~Base();
	}

protected:
	int _d;
};


void TestDerived()
{
	Derived d(1, 2);
}

int main()
{
	TestDerived();
	return 0;
}

class Base
{
public:
	Base()
	{
		cout << "Base::Base()" << endl;
	}

	~Base()
	{
		cout << "Base::~Base()" << endl;
	}
protected:
	int _b;
};

class Derived : public Base
{
public:
	Derived()
		//: Base()
	{
		cout << "Derived::Derived()" << endl;
	}

	~Derived()
	{
		cout << "Derived::~Derived()" << endl;
		// call Base::~Base();
	}

protected:
	int _d;
};


void TestDerived()
{
	Derived d;
}

int main()
{
	TestDerived();
	return 0;
}

class B
{
public:
	B(int b)
		: _b(b)
	{
		_count++;
	}

	B(const B& b)
		: _b(b._b)
	{
		_count++;
	}

	~B()
	{
		--_count;
	}

	int _b;
	static int _count;
};

int B::_count = 0;


class D1 : public B
{
public:
	D1(int b)
		: B(b)
	{}
	void func()
	{
		cout << _count << endl;
	}
};

class D2 : public B
{
public:
	D2(int b)
		: B(b)
	{}
	void func()
	{
		cout << _count << endl;
	}
};

int main()
{
 cout << sizeof(D1) << endl;  // 大小为 4 ---------静态成员不包含在类的对象中，计算大小也不用计算他
	D1 d1(2);
	D2 d2(1);
	cout << d1._count << endl;
	cout << d2._count << endl;

	cout << &d1._count << endl;
	cout << &d2._count << endl;


	cout << &D1::_count << endl;
	cout << &D2::_count << endl;
	return 0;
}

class B
{
	friend void Print(const B& b);
public:
	B(int b)
		: _b(b)
	{}

protected:
	int _b;
};

class D : public B
{
public:
	D(int b, int d)
		: B(b)
		, _d(d)
	{}

protected:
	int _d;
};


void Print(const B& b)
{
	cout << b._b << endl;

	D d(1, 2);
	
	cout << d._b << endl;

	// 编译报错
	// 友元关系不能继承
	 //cout << d._d << endl;
}

int main()
{
	B b(1);

	Print(b);
	return 0;
	
}


class B1
{
public:
	int _b1;
};

class B2
{
public:
	int _b2;
};
注意：如果有多个基类，每个基类前的继承权限不能省略
如果没有显式给出，则继承权限就是默认的
class D :public B2 , public B1
{
	void set()
	{
		_b1 = 10;
		_b2 = 11;
		_d  = 12;
	}

public:
	int _d;
};

class E : public D
{
	void set()
	{
		_b1 = 1;
		_b2 = 2;
		_d = 3;
	}
};


int main()
{
	cout << sizeof(D) << endl;

	D d;
	d._b1 = 1;
	d._b2 = 2;
	d._d = 3;
	return 0;
}


class B
{
public:
	B(int b)
		:_b(b)
	{}

	int _b;
};

class C1 : public B
{
public:
	C1(int b, int c)
		: B(b)
		, _c1(c)
	{}

	int _c1;
};

class C2 : public B
{
public:
	C2(int b, int c)
		: B(b)
		, _c2(c)
	{}

	int _c2;
};

class D : public C1, public C2
{
public:
	D(int b1, int c1, int b2, int c2, int d)
		: C1(b1, c1)
		, C2(b2, c2)
		, _d(d)
	{}

	int _d;
};

int main()
{
	D d(1, 2, 3, 4, 5);
	d.C1::_b = 10;
	d.C2::_b = 100;

	return 0;
}

class B
{
public:
	void func()
	{
		cout << "B::func()" << endl;
	}

	int _b;
};

class C1 : public B
{
public:
	int _c1;
};

class C2 : public B
{
public:
	int _c2;
};

class D : public C1, public C2
{
public:
	int _d;
};


int main()
{
	cout << sizeof(D) << endl;

	D d;

	// 菱形继承二义性问题
	// d._b = 1;
	// d.func();

	// 如何解决？
	// 1. 让访问明确化---当d访问最顶层成员时候，直接加上其父类的名称
	d.C1::func();
	d.C2::func();
	d.C1::_b = 1;
	d._c1 = 2;
	
	d._c2 = 4;

	d._d = 5;

	// 2. 解决方式：让最顶层基类中成员在d中只存储一份
	//    菱形虚拟继承
	return 0;
}

// 什么是虚拟继承？
class B
{
public:
	void func()
	{
		cout << "B::func()" << endl;
	}

	int _b;
};

class D : virtual public B
{
public:
	D()
	{
		_d = 10;
	}
	int _d;
};

int main()
{
	cout << sizeof(D) << endl;

	D  d;
	d._b = 1;
	d._d = 2;
	return 0;
}


// 注意：一般虚拟继承不会像上述方式使用
// 虚拟继承主要是用来实现菱形虚拟继承的，目的：解决菱形继承中的二义性问题

class B
{
public:
	void func()
	{
		cout << "B::func()" << endl;
	}

	int _b;
};

class C1 : virtual public B
{
public:
	int _c1;
};

class C2 : virtual public B
{
public:
	int _c2;
};

class D : public C1, public C2
{
public:
	int _d;
};


int main()
{
	cout << sizeof(D) << endl;

	D d;
	d._b = 1;
	d._c1 = 2;
	d._c2 = 3;
	d._d = 4;

	采用菱形虚拟继承解决：菱形继承中二义性问题
	将最顶层B中的成员在D中只继承一份
	d._b = 1;
	d.func();


	C1& c1 = d;
	c1._b = 10;

	C2& c2 = d;
	c2._b = 20;

	d._b = 30;

	return 0;
}
